#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "rbtree.h"

int main(){
    rbtree tree = new_tree(0,0);
    rbnode node;
    int     i;
    double  x,y;
    char    *cmd = malloc(sizeof(char)*100);
    printf("enter seed: ");
    scanf("%d", &i);
    srand(i);
    for (i=0;i<30;i++)insert(tree, rand()%1000, i);
    for(int iterations=0; iterations<1000; iterations++){
	printf("Iteration: %d\n",iterations);
	printf("NTree: %d\n", ntree(tree));
	print_tree(tree);
	scanf("%s", cmd);
	if ( strcmp(cmd, "insert")==0 ){
	    scanf("%lf %lf", &x, &y);
	    insert(tree, x, y);
	}else if ( strcmp(cmd, "delete")==0 ){
	    scanf("%lf", &x);
	    delete(tree, x);
	}else if (strcmp(cmd, "find")==0){
	    scanf("%lf", &x);
	    if ( find(tree, x, &node) != found )
		printf("node not found\n");
	    else
		printf("%lf,%lf\n", node->value, node->content);
	}else if( strcmp(cmd, "get")==0){
	    scanf("%d", &i);
	    node = get(tree, i);
	    if ( node==NULL )
		printf("index out of range\n");
	    else
		printf("%lf,%lf\n", node->value, node->content);
	}else if( strcmp(cmd, "testget")==0){
	    for (i=0; i<tree->n; i++){
		node = get(tree, i);
		printf("%d %f\n", i, node->value);
	    }
	}else if( strcmp(cmd, "quit")==0)
	    break;
	else
	    printf("wrong commad: '%s'\n", cmd);
    }
    return 0;
}
